mysql触发器使用if..then sql elseif then end if; 转自 吴大哥

您所在的位置:网站首页 mysql中使用if else mysql触发器使用if..then sql elseif then end if; 转自 吴大哥

mysql触发器使用if..then sql elseif then end if; 转自 吴大哥

2023-11-02 06:00| 来源: 网络整理| 查看: 265

    mysql 触发器 if then elseif else 的运用

自己第一次写触发,想使用两个三个条件语句并列使用,但是不管怎么写都保存不了,最后看了吴大哥的博文,试了是if..then ...end if;中使用if并列是可以。

我是不知道是为什么,有大神知道可以详解。下面贴上我的触发器:

  

DROP TRIGGER `down`; CREATE DEFINER=`root`@`localhost` TRIGGER `down` BEFORE UPDATE ON `ne` FOR EACH ROW if '1'='1' then #Step 1 #拆除报警: if new.down_status = '1' then insert into ne_alarm_his (ID, alarm_id, company_id, ne_no , hierarchy , ne_model_id, alarm_type_id , alarm_level, alarm_up_time , alarm_last_time, occur_count, ack_user, ack_time, ack_res, ack_remark, alarmtime, areaid, alermaddress , gps_lon , gps_lat , car_id,AuditStatus ,status) values (uuid(), getSerialNumber('报警编号'),new.company_id, new.ne_no ,new.hierarchy, new.ne_model_id , 1, null, new.down_starttime , now() , null , null , null , null , null , now() , null , new.geographical_address2 , new.gps_lon2 ,new.gps_lat2, new.car_id, '0',null ); end if; #Step 2: #低电量报警 if new.low_power_status = '1' then insert into ne_alarm_his (ID, alarm_id, company_id, ne_no , hierarchy , ne_model_id, alarm_type_id , alarm_level, alarm_up_time , alarm_last_time, occur_count, ack_user, ack_time, ack_res, ack_remark, alarmtime, areaid, alermaddress , gps_lon , gps_lat , car_id,AuditStatus , status) values (uuid(), getSerialNumber('报警编号'), new.company_id, new.ne_no, new.hierarchy, new.ne_model_id , 5, null, new.low_power_starttime , now() , null , null , null , null , null , now() , null , new.geographical_address2 , new.gps_lon2 ,new.gps_lat2, new.car_id, '0', null ); end if; #Step 3: #断电报警 if new.pwr_down_status = '1' then insert into ne_alarm_his (ID, alarm_id, company_id, ne_no , hierarchy , ne_model_id, alarm_type_id , alarm_level, alarm_up_time , alarm_last_time, occur_count, ack_user, ack_time, ack_res, ack_remark, alarmtime, areaid, alermaddress , gps_lon , gps_lat , car_id, AuditStatus ,status) values (uuid(), getSerialNumber('报警编号'), new.company_id, new.ne_no, new.hierarchy, new.ne_model_id , 6, null, new.pwr_down_starttime , now() , null , null , null , null , null , now() , null , new.geographical_address2 , new.gps_lon2 ,new.gps_lat2, new.car_id, '0', null ); end if; end if;

 

 判断是否包含字符串,

  使用函数   locate('admin','ad')    

  如果包含,返回>0的数,否则返回0

set @alarmType = new.alerttypeset; if (locate(@alarmType , '9'))>0 and new.run_status !=old.run_status and new.run_status = '1' then ..sql... end if;

 mysql中字符串的拼接不能使用加号+,用concat函数:

set @vcommandPara =concat('&Uconfig=','replynumber');

 

 

 下面是吴大哥的博文:

create procedure dbname.proc_getGrade (stu_no varchar(20),cour_no varchar(10)) BEGIN declare stu_grade float; select grade into stu_grade from grade where student_no=stu_no and course_no=cour_no; if stu_grade>=90 then select stu_grade,'A'; elseif stu_grade=80 then select stu_grade,'B'; elseif stu_grade=70 then select stu_grade,'C'; elseif stu_grade70 and stu_grade>=60 then select stu_grade,'D'; else select stu_grade,'E'; end if; END

案列

CREATE TRIGGER cfq AFTER INSERT ON hc_alarmFOR EACH ROWBEGINdeclare is_alarm int; declare telephoneone VARCHAR(20); //定义declare telephonetwo VARCHAR(20); declare telephonethree VARCHAR(20); declare telephonefour VARCHAR(20); declare telephonefive VARCHAR(20); SELECT gwsbbj.is_alarm into is_alarm FROM gwsbbj,(SELECT a.mac from hc_gw a ,hc_std b where b.gwid=a.id and b.id=new.stdid) c   //赋值 where new.stdid=gwsbbj.stdid and gwsbbj.gwid=c.mac;SELECT k.telephoneone, k.telephonetwo,k.telephonethree,k.telephonefour,k.telephonefive into telephoneone,telephonetwo,telephonethree,telephonefour,telephonefive //多个赋值from hc_gw f ,hc_std g ,hc_install_image k where g.gwid=f.id and g.id=new.stdid and f.mac=k.gwmac;if NEW.triggertype = 'std' and (NEW.event ='017' or NEW.event ='015') and is_alarm=0 then   // 一个if 配一个end if  UPDATE gwsbbj set gwsbbj.is_alarm =1 where gwsbbj.stdid=new.stdid;      if telephoneone is NOT NULL then       insert into bdnr(content,tel) values( CONCAT('安装在',new.trigger,'发生了',new.title),telephoneone);      END if;     if telephonetwo !='' then     insert into bdnr(content,tel) values( CONCAT('安装在',new.trigger,'发生了',new.title),telephonetwo);    END if;   if telephonethree !='' then    insert into bdnr(content,tel) values( CONCAT('安装在',new.trigger,'发生了',new.title),telephonethree);   END if;  if telephonefour !='' then    insert into bdnr(content,tel) values( CONCAT('安装在',new.trigger,'发生了',new.title),telephonefour);   END if;   if telephonefive !='' then    insert into bdnr(content,tel) values( CONCAT('安装在',new.trigger,'发生了',new.title),telephonefive);   END if;elseif NEW.triggertype = 'std' and (NEW.event ='017' or NEW.event ='015') and is_alarm=1then UPDATE gwsbbj set gwsbbj.is_alarm=1 where gwsbbj.stdid=new.stdid;END if;END;

if...then{ if...then{}end if; if...then{}end if;...}elseif...then..end if;  

CREATE TRIGGER   --触发器必须有名字,最多64个字符,可能后面会附有分隔符.它和MySQL中其他对象的命名方式基本相象.{ BEFORE | AFTER }  --触发器有执行的时间设置:可以设置为事件发生前或后。{ INSERT | UPDATE | DELETE }  --同样也能设定触发的事件:它们可以在执行insert、update或delete的过程中触发。ON   --触发器是属于某一个表的:当在这个表上执行插入、 更新或删除操作的时候就导致触发器的激活. 我们不能给同一张表的同一个事件安排两个触发器。FOR EACH ROW  --触发器的执行间隔:FOR EACH ROW子句通知触发器 每隔一行执行一次动作,而不是对整个表执行一次。  --触发器包含所要触发的SQL语句:这里的语句可以是任何合法的语句, 包括复合语句,但是这里的语句受的限制和函数的一样。

--你必须拥有相当大的权限才能创建触发器(CREATE TRIGGER),如果你已经是Root用户,那么就足够了。这跟SQL的标准有所不同。

~~实例~~ example1:

创建表tab1

1 2 3 4 DROP TABLE IF EXISTS tab1; CREATE TABLE tab1(     tab1_id varchar(11) );

创建表tab2

1 2 3 4 DROP TABLE IF EXISTS tab2; CREATE TABLE tab2(     tab2_id varchar(11) );

创建触发器:t_afterinsert_on_tab1

作用:增加tab1表记录后自动将记录增加到tab2表中

1 2 3 4 5 6 7 DROP TRIGGER IF EXISTS t_afterinsert_on_tab1; CREATE TRIGGER t_afterinsert_on_tab1 AFTER INSERT ON tab1 FOR EACH ROW BEGIN      insert into tab2(tab2_id) values(new.tab1_id); END;

测试一下

1 INSERT INTO tab1(tab1_id) values('0001');

看看结果

1 2 SELECT * FROM tab1; SELECT * FROM tab2; example2:

创建触发器:t_afterdelete_on_tab1

作用:删除tab1表记录后自动将tab2表中对应的记录删去

1 2   4 5 6 7 DROP TRIGGER IF EXISTS t_afterdelete_on_tab1; CREATE TRIGGER t_afterdelete_on_tab1 AFTER DELETE ON tab1 FOR EACH ROW BEGIN       delete from tab2 where tab2_id=old.tab1_id; END;

测试一下

  DELETE FROM tab1 WHERE tab1_id='0001';

看看结果

 

1 2 SELECT * FROM tab1; SELECT * FROM tab2;


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3